<template>
  <div class="song-list">
    <van-nav-bar
      title="歌单详情"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="header">
      <img :src="song.logo" alt="" />
      <h4>{{ song.dissname }}</h4>
      <div class="nick">
        <img :src="song.headurl" alt="" />
        <span>{{ song.nickname }}</span>
      </div>
      <span>播放量：{{ song.visitnum | numFilter(1) }}万</span>
    </div>
    <div class="list">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div v-for="(item, index) in list" :key="index">
          <h3>{{ item.songname }}</h3>
          <div class="singer">
            <span v-for="(singer, index) in item.singer" :key="index">
              {{ singer.name }}
              <span v-show="index != item.singer.length - 1">/</span>
            </span>
            <span v-show="item.albumname">
              <span>·</span>
              {{ item.albumname }}
            </span>
          </div>
        </div>
      </van-list>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      song: {
        logo: "", //歌单logo图片
        dissname: "", //歌单标题
        headurl: "", //歌单制作者头像
        nickname: "", //歌单制作者的昵称
        visitnum: 0, //歌单播放量
      },
      songlist: [], //API获取到的歌曲列表
      songnum: 0, //歌曲列表数目
      list: [], //vant的list列表数据
      loading: false, //加载状态
      finished: false, //是否完成
    };
  },
  created() {
    this.getListData();
  },
  methods: {
    getListData() {
      this.$request("get", "/songlist", { id: this.$route.params.id }).then(
        (res) => {
          console.log(res);
          this.song.logo = res.data.logo;
          this.song.dissname = res.data.dissname;
          this.song.headurl = res.data.headurl;
          this.song.nickname = res.data.nickname;
          this.song.visitnum = res.data.visitnum;
          this.songlist = res.data.songlist;
          this.songnum = res.data.songnum;
        }
      );
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        this.list = this.list.concat(this.songlist.splice(0,10))
        // console.log(this.list)
        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= this.songnum) {
          this.finished = true;
        }
      }, 1000);
    },
  },
};
</script>
<style scoped>
/*歌单头部样式*/
.header>img{
  border-radius: 1rem;
  width: 50%;
}
.nick{
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 1rem;
}
.nick > img {
  height: 2rem;
  border-radius: 50%;
  margin-right: 0.5rem;
}
.header > span {
  height: 2rem;
  font-size: 0.8rem;
  line-height: 2rem;
}
.list{
  margin: 0 1rem;
}
.list h3{
  text-align: left;
}
.singer{
  text-align: left;
}
.singer span{
  font-size: 0.8rem;
  color: rgb(160, 160, 180);
}
.singer>span:nth-last-child(1)>span:first-child{
  color: red;
  font-weight: 900;
  margin: 0 0.3rem;
}
</style>